package com.lzh.seatrend.common;


import com.lzh.seatrend.common.constant.ResultCode;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @author lzh
 */
public class Result {

    private boolean status;
    private int code;
    private Object data;
    private String message;
    private int total;

    public Result() {
    }


    public Result(String message) {
        this.message = message;
    }

    public Result(int code, Object data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }

    public Result setMsg(ResultCode resultCode){
        this.status = resultCode.getStatus();
        this.code = resultCode.getCode();
        this.data = "no content";
        this.message = resultCode.getMsg();
        return this;
    }

    public Result setMsg(ResultCode resultCode, Object data) {
        this.status = resultCode.getStatus();
        this.code = resultCode.getCode();
        this.message = resultCode.getMsg();
        if (data instanceof Page){
            this.data = ((Page<?>) data).getContent();
            this.total = (int) ((Page<?>) data).getTotalElements();
        }
        else if (data instanceof List){
            this.data = data;
            this.total = ((List<?>) data).size();
        }
        else {
            this.data = data;
        }
        return this;
    }
}
